<?php

namespace app\admin\logic;

use app\common\model\mysql\AccountModel;
use app\common\logic\BaseLogic;

class PersonalLogic extends BaseLogic
{
    public function index($request)
    {
        $info = AccountModel::where(['id' => $request->account['id']])->find();
        return view('Personal/personal', compact('info'));
    }

    public function save($request)
    {
        $param = $request->param();

        $inputData = [];

        if ($request->has('headimg.name', 'file', true)) {
            $file = request()->file('headimg');
            // 上传到本地服务器
            $inputData['headimg'] = \think\facade\Filesystem::disk('public')->putFile('headimg', $file);
        }

        !empty($param['password']) ? $inputData['password'] = \app\common\facade\encrypt\Password::encryptPassword($param['password']) : false;

        try {
            AccountModel::editData($inputData, ['id' => $request->account['id']]);
        } catch (\Exception $e) {
            return $this->responseError('保存失败');
        }
        return $this->responseSuccess('保存成功，重新登录生效');
    }

}